package com.gitee.ricesugar.borrow;

import com.gitee.ricesugar.borrow.module.AddBorrow;
import com.gitee.ricesugar.borrow.module.ListBorrow;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

public class BorrowPanel {
    private static JPanel contentPanel;

    public static JPanel createBorrowPanel() throws IOException {
        //新建主布局
        JPanel borrowPanel = new JPanel();
        borrowPanel.setLayout(new BorderLayout());

        //添加侧边栏
        JPanel sidebar = new JPanel();
        sidebar.setLayout(new GridLayout(2, 1));

        JButton addBorrowButton = new JButton("添加记录");
        JButton borrowListButton = new JButton("借阅记录");
        addBorrowButton.setPreferredSize(new Dimension(96, 40));

        sidebar.add(addBorrowButton);
        sidebar.add(borrowListButton);

        //添加每个页面
        contentPanel = new JPanel();
        contentPanel.setLayout(new CardLayout());
        JPanel addBorrowPage = new AddBorrow();
        JPanel borrowListPage = new ListBorrow();
        contentPanel.add(addBorrowPage, "addBorrowPage");
        contentPanel.add(borrowListPage, "borrowListPage");
        showPanel("borrowListPage");

        addBorrowButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showPanel( "addBorrowPage");
            }
        });
        borrowListButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showPanel("borrowListPage");
            }
        });

        borrowPanel.add(sidebar, BorderLayout.WEST);
        borrowPanel.add(contentPanel, BorderLayout.CENTER);

        //返回主布局
        return borrowPanel;
    }

    private static void showPanel(String panelName) {
        CardLayout cardLayout = (CardLayout) contentPanel.getLayout();
        cardLayout.show(contentPanel, panelName);
    }
}
